#Plots 
#Alan Yan and Rachel Bernhard
#3-19-2020
# updated 4-13/-020

#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse,
       gridExtra,
       cowplot)

#### Figure 1 ####
## Both studies with by study breakdown
# Estimates generated in Stata
sample <- c(rep("Full Sample", 4), rep("Study 1", 4), rep("Study 2", 4))
Treatment <- rep(c("Female\nName", "Male\nName", "No\nName", "Constant"), 3)
Estimate <- c(.177, -.097, .148, .232, .151, -.014, .074, .191, .199, -.164, .208, .228)
SEs <- c(.042, .031, .041, .028, .020, .018, .026, .012, .059, .039, .059, .035)
ps <- c(0, NA, 1, 0, 5, 1, 1, 0, 5, 1, 1, 0)
offdf <- data.frame(sample, Treatment, Estimate, SEs, ps)
offdf$n <- c(rep(135587, 4), rep(60356, 4), rep(75231, 4))
offdf$headers <- paste0(offdf$sample, "\n(n = ", offdf$n, ")", sep="")
offdf$fcolor <- ifelse(offdf$Treatment=="Female\nName", "bright","grey")
offdf$fcolor <- factor(offdf$fcolor)

# Plot 
offensive_binarized <- ggplot(data = offdf[offdf$Treatment!="Constant",], 
                             aes(x = Treatment, y = Estimate, fill = fcolor)) + 
  facet_wrap(~headers, ncol=4) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           width=.5,
           size=.3) +
  geom_errorbar(aes(ymin=Estimate-1.96*SEs, ymax=Estimate+1.96*SEs), width = .025,
                position=position_dodge(.9)) +
  ylab("Percentage Point Effect") +
  xlab("") +
  ggtitle("") +
  theme_minimal() + 
  theme(axis.text=element_text(size=rel(1), face = "bold"),
        axis.title.x=element_text(size=rel(1.2), margin=margin(5,0,0,0)),
        axis.title.y=element_text(size=rel(1.2), margin=margin(0,5,0,0)),
        strip.text = element_text(size=rel(1.2)),
        plot.margin = unit(c(-.25,.25,-.25,.25), "cm"),
        legend.position = "none") +
  scale_fill_manual(values=c("#8D1BBD", "grey70"))
ggsave(offensive_binarized, file = "05-Plots/offensive_binarized.pdf", height=2.5, width=7.5)

#### Figure 2 ####
## Both studies with by study split
sample <- c(rep("Full Sample", 4), rep("Study 1", 4), rep("Study 2", 4))
Treatment <- rep(c("Female\nName", "Male\nName", "No\nName", "Constant"), 3)
Estimate <- c(.117, -.053, .057, .147, .096, .020, .040, .106, .134, -.111, .069, .154)
SEs <- c(.040, .036, .053, .087, .045, .039, .041, .026, .048, .032, .045, .029)
ps <- c(0, NA, 1, 0, 1, NA, 5, 0, 1, NA, 5, 1)
sildf <- data.frame(sample, Treatment, Estimate, SEs, ps)
sildf$n <- ifelse(sildf$sample=="Full Sample", 135587,
                  ifelse(sildf$sample=="Study 1", 60356, 75231))
sildf$headers <- paste0(sildf$sample, "\n(n = ", sildf$n, ")", sep="")
sildf$fcolor <- ifelse(sildf$Treatment=="Female\nName", "bright","grey")
sildf$fcolor <- factor(sildf$fcolor)

# Plot 
silencing_by_study <- ggplot(data = sildf[sildf$Treatment!="Constant",], 
                    aes(x = Treatment, y = Estimate, fill = fcolor)) + 
  facet_wrap(~headers, ncol=3) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           width=.5,
           size=.3) +
  geom_errorbar(aes(ymin=Estimate-1.96*SEs, ymax=Estimate+1.96*SEs), width = .025,
                position=position_dodge(.9)) +
  ylab("Percentage Point Effect") +
  xlab("") +
  ggtitle("") +
  theme_minimal() + 
  theme(axis.text=element_text(size=rel(1), face = "bold"),
        axis.title.x=element_text(size=rel(1.2), margin=margin(5,0,0,0)),
        axis.title.y=element_text(size=rel(1.2), margin=margin(0,5,0,0)),
        strip.text = element_text(size=rel(1.2)),
        plot.margin = unit(c(-.25,.25,-.25,.25), "cm"),
        legend.position = "none") +
  scale_fill_manual(values=c("#8D1BBD", "grey70"))
silencing_by_study

ggsave(silencing_by_study, file = "05-Plots/silencing_by_study.pdf", height=2.5, width=7.5)

#### Figure 3 #####
## Both studies with by study split
sample <- c(rep("Full Sample", 4), rep("Study 1", 4), rep("Study 2", 4))
Treatment <- rep(c("Female\nName", "Male\nName", "No\nName", "Constant"), 3)
Estimate <- c(.383, -.334, .111, .484, .579, -.313, .023, 1.754, .227, -.350, .182, .509)
SEs <- c(.085, .072, .081, .055, .163, .144, .151, .107, .081, .059, .080, .052)
ps <- c(0, NA, 1, 0, 1, NA, 5, 0, 1, NA, 5, 1)
sildf <- data.frame(sample, Treatment, Estimate, SEs, ps)
sildf$n <- ifelse(sildf$sample=="Full Sample", 135587,
                  ifelse(sildf$sample=="Study 1", 60356, 75231))
sildf$headers <- paste0(sildf$sample, "\n(n = ", sildf$n, ")", sep="")
sildf$fcolor <- ifelse(sildf$Treatment=="Female\nName", "bright","grey")
sildf$fcolor <- factor(sildf$fcolor)

# Plot 
withdrawal_by_study <- ggplot(data = sildf[sildf$Treatment!="Constant",], 
                             aes(x = Treatment, y = Estimate, fill = fcolor)) + 
  facet_wrap(~headers, ncol=3) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           width=.5,
           size=.3) +
  geom_errorbar(aes(ymin=Estimate-1.96*SEs, ymax=Estimate+1.96*SEs), width = .025,
                position=position_dodge(.9)) +
  ylab("Percentage Point Effect") +
  xlab("") +
  ggtitle("") +
  theme_minimal() + 
  theme(axis.text=element_text(size=rel(1), face = "bold"),
        axis.title.x=element_text(size=rel(1.2), margin=margin(5,0,0,0)),
        axis.title.y=element_text(size=rel(1.2), margin=margin(0,5,0,0)),
        strip.text = element_text(size=rel(1.2)),
        plot.margin = unit(c(-.25,.25,-.25,.25), "cm"),
        legend.position = "none") +
  scale_fill_manual(values=c("#8D1BBD", "grey70"))
withdrawal_by_study

ggsave(withdrawal_by_study, file = "05-Plots/withdrawal_by_study.pdf", height=2.5, width=7.5)

#### Figure S2 ####
# Study 2 only
sample <- c(rep("Study 2", 4))
Treatment <- rep(c("Female\nName", "Male\nName", "No\nName", "Constant"), 1)
Estimate <- c(.393, -.355, .413, .554)
SEs <- c(.067, .048, .068, .041)
ps <- c(rep(0,4))
discdf <- data.frame(sample, Treatment, Estimate, SEs, ps)
discdf$n <- ifelse(discdf$sample=="Study 2", 75231,
                   ifelse(discdf$sample=="Women Respondents", 41143, 29321))
discdf$headers <- paste0(discdf$sample, "\n(n = ", discdf$n, ")", sep="")
discdf$fcolor <- ifelse(discdf$Treatment=="Female\nName", "bright","grey")
discdf$fcolor <- factor(discdf$fcolor)

# Plot 
discouraging_main <- ggplot(data = discdf[discdf$Treatment!="Constant",], 
                            aes(x = Treatment, y = Estimate, fill = fcolor)) + 
  facet_wrap(~headers, ncol=3) + 
  geom_bar(position=position_dodge(), stat="identity",
           colour="black", 
           width=.5,
           size=.3) +
  geom_errorbar(aes(ymin=Estimate-1.96*SEs, ymax=Estimate+1.96*SEs), width = .025,
                position=position_dodge(.9)) +
  ylab("Percentage Point Effect") +
  xlab("") +
  ggtitle("") +
  theme_minimal() + 
  theme(axis.text=element_text(size=rel(1), face = "bold"),
        axis.title.x=element_text(size=rel(1.2), margin=margin(5,0,0,0)),
        axis.title.y=element_text(size=rel(1.2), margin=margin(0,5,0,0)),
        strip.text = element_text(size=rel(1.2)),
        plot.margin = unit(c(-.25,.25,-.25,.25), "cm"),
        legend.position = "none") +
  scale_fill_manual(values=c("#8D1BBD", "grey70"))

ggsave(discouraging_main, file = "05-Plots/discouraging_main.pdf", height=2.5, width=2.75)
